
import React from 'react';

import styles from './GoodsList.css';
import {Table, Divider, Tag, message} from 'antd';
import UserService from "../../business/user/UserService";

const rowSelection = {
  onChange: (selectedRowKeys, selectedRows) => {
  },
  getCheckboxProps: record => ({
    disabled: record.name === 'Disabled User', // Column configuration not to be checked
    name: record.name,
  }),
};

class AdminList extends React.Component{

  constructor(props) {
    super(props);
    this.state ={
      giftData:[],
    }
    this.userService =new UserService();
    this.callBackQueryList = this.callBackQueryList.bind(this);
    this.enableAccount =this.enableAccount.bind(this);
    this.disableAccount = this.disableAccount.bind(this);
    this.callBackEODList =this.callBackEODList.bind(this);

  }
  userService:UserService;
  state ={
    giftData:[],
  }
  columns = [{
    title: '用户名',
    dataIndex: 'username',
    key: 'username',
    render: text => <a href="javascript:;">{text}</a>,
  }, {
    title: '角色',
    dataIndex: 'role',
    key: 'role',
    render:(text, record)=>{
      if(text == 'ROLE_RP_SYSTEM') {
        return "积分授予系统";
      }else if(text == 'ROLE_RP_ADMIN'){
        return "积分操作管理员";
      }else if(text == 'ROLE_CP_ADMIN'){
        return "积分商城管理员";
      }else if(text == 'ROLE_ROOT'){
        return "账户管理员";
      }else if(text == 'ROLE_USER'){
        return "普通用户";
      }else{
        return text;
      }
    }
  }, {
    title: '组织',
    dataIndex: 'host',
    key: 'host',
  }, {
    title: '状态',
    dataIndex: 'enabled',
    key: 'enabled',
    render:(text, record)=>{
      if(text){
        return "已激活";
      }else{
        return "待激活";
      }

    }
  }, {
    title: '操作',
    dataIndex: 'enabled',
    key: 'action',
    render: (text, record) => {
      if(text){
        return (<span>
      <a href="javascript:;" onClick={()=>this.disableAccount(record.username)}>冻结</a>
    </span>);
      }else{
        return (<span>
      <a href="javascript:;" onClick={()=>this.enableAccount(record.username)}>激活</a>
    </span>);
      }

    },
  }];

  enableAccount(username:string){
    let token  = sessionStorage.getItem("adminLoginToken");
    this.userService.enableAccount(username,token,this.callBackEODList);
  }
  disableAccount(username:string){
    let token  = sessionStorage.getItem("adminLoginToken");
    this.userService.disableAccount(username,token,this.callBackEODList);

  }

  callBackEODList(res,isSuccess){
    if(!isSuccess || res.code !="200"){
      message.info(res.message);
    }else{
      this.componentWillMount();
    }
  }

  callBackQueryList(res,isSuccess){
    if(!isSuccess || res.code !="200"){
      message.info(res.message);
    }else{
      this.setState({giftData: res.data});
    }
  }

  componentWillMount(){
    console.info("adminListPage http start");
    let data ={
      username:sessionStorage.getItem('username')
    };
    let token  = sessionStorage.getItem("adminLoginToken");
    this.userService.queryAdminList(data,token,this.callBackQueryList);
  }


  show(giftData:any[]){
    if(giftData.length !=0){
     return (<Table rowSelection={rowSelection} columns={this.columns} dataSource={giftData} rowKey={list=>list.username} />);
    }else{
      return "";
    }
  }

  render() {
    const {giftData} = this.state;
    return (
      <div className={styles.normal}>
        {this.show(giftData)}
      </div>
    );
  }
}

export default AdminList;
